package com.mo.demo;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;

import com.hzmct.enjoysdk.api.EnjoySDK;
import com.hzmct.enjoysdk.transform.McErrorCode;
import com.hzmct.enjoysdk.transform.McStateCode;
import com.mc.android.mcinstall.AppRule;
import com.mc.android.mcinstall.McWhiteApp;
import com.mc.android.mckeepalive.McKeepAliveApp;

import java.util.ArrayList;

public class Enjoy {

    static boolean force = false;
    static Handler h = new Handler(Looper.getMainLooper());

    public static void showNavi() {
        force = true;
        setNavigation(true);
    }

    public static boolean isShowNavi() {
        return force;
    }

    public static void setNavigation(boolean show) {
        try {
            if (force && !show) {
                return;
            }
            EnjoySDK enjoySDK = new EnjoySDK(MyApp.getApp());

            int state = show ? 1 : 0;
            enjoySDK.setNavigationBarShowStatus(state);
            if (show) {
                if (Build.VERSION.SDK_INT >= 30) {
                    enjoySDK.disableStatusBarItem(McStateCode.UI_DISABLE_HOME
                            | McStateCode.UI_DISABLE_RECENT
                            | McStateCode.UI_DISABLE_EXPAND
                            | McStateCode.UI_DISABLE_NOTIFICATION_ICONS
                            | McStateCode.UI_DISABLE_CLOCK
                            | McStateCode.UI_DISABLE_SYSTEM_INFO);
                } else {
                    enjoySDK.disableStatusBarItem(McStateCode.UI_DISABLE_NONE);
                    new Handler(Looper.getMainLooper()).postDelayed(() -> {
                        try {
                            enjoySDK.disableStatusBarItem(McStateCode.UI_DISABLE_HOME
                                    | McStateCode.UI_DISABLE_RECENT
                                    | McStateCode.UI_DISABLE_EXPAND
                                    | McStateCode.UI_DISABLE_NOTIFICATION_ICONS
                                    | McStateCode.UI_DISABLE_CLOCK
                                    | McStateCode.UI_DISABLE_SYSTEM_INFO);
                        } catch (Throwable e) {
                            e.printStackTrace();
                        }
                    }, 300);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static boolean setScreen(Context context, int degree) {
        context = context.getApplicationContext();
        try {
            EnjoySDK enjoySDK = new EnjoySDK(context);
            boolean bo = enjoySDK.checkSafeProgramOfSelf();
            if (!bo) {
                int i = enjoySDK.setSecurePasswd("Abc12345", "Abc12345");
                i = enjoySDK.registSafeProgram("Abc12345");
                System.currentTimeMillis();
            }
            enjoySDK.setSystemRotation(degree);
            enjoySDK.setHomePackage(context.getPackageName());
            enjoySDK.setStatusBarShowStatus(true);
            if (force) {
                setNavigation(true);
            } else {
                enjoySDK.setNavigationBarShowStatus(0);
                enjoySDK.disableStatusBarItem(
                        McStateCode.UI_DISABLE_EXPAND
                                | McStateCode.UI_DISABLE_NOTIFICATION_ICONS
                                | McStateCode.UI_DISABLE_CLOCK
                                | McStateCode.UI_DISABLE_SYSTEM_INFO);
            }
            return true;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void keepAlive(Context c) {
        c = c.getApplicationContext();
        try {
            EnjoySDK sdk = new EnjoySDK(c);
//            if (!sdk.isEnableWhiteRules()) {
//                sdk.whiteRulesSwitch(true);
//            }
            sdk.whiteRulesSwitch(false);
            if ("v1".equalsIgnoreCase(sdk.getEnjoyApiSupportVersion())) {
                if (sdk.isUninstallAllow(c.getPackageName())) {
                    ArrayList<McWhiteApp> tmp = new ArrayList();
                    McWhiteApp app = new McWhiteApp();
                    app.setPackageName(c.getPackageName());
                    app.setAllowUninstall(false);
                    app.setOpenAfterInstall(true);
                    app.setInstallMode(McWhiteApp.INSTALL_MODE_FOREGROUND);
                    tmp.add(app);
                    sdk.addWhiteList(tmp);
                }
            } else {
                boolean found = false;
                ArrayList<AppRule> tmp = sdk.getNotUninstallList();
                if (tmp != null) {
                    String pkg = c.getPackageName();
                    for (AppRule rule : tmp) {
                        if (pkg.equals(rule.getPackageName())) {
                            found = true;
                            break;
                        }
                    }
                }
                if (!found) {
                    AppRule rule = new AppRule(c.getPackageName(), false);
                    sdk.addWhiteRules(rule);
                    sdk.addNotUninstallRules(new AppRule(c.getPackageName(), false));
                }
            }
            if (!sdk.isKeepAliveOpen()) {
                if (sdk.EnableKeepAlive(true) != McErrorCode.ENJOY_COMMON_SUCCESSFUL) {
                    return;
                }
            }
            sdk.setKeepAliveAfterAppStart(true);
            if (sdk.isKeepAliveApp(c.getPackageName()) == McErrorCode.ENJOY_KEEP_ALIVE_MANAGER_IS_KEEP_ALIVE) {
                return;
            }
            ArrayList<McKeepAliveApp> list = new ArrayList<>();
            McKeepAliveApp app = new McKeepAliveApp();
            app.setPackagename(c.getPackageName());
            app.setBackstage(true);
            app.setPriority(1);
            app.setInterval(5);
            list.add(app);
            sdk.addKeepAliveAPP(list);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static boolean startLauncher() {
        try {
            EnjoySDK sdk = new EnjoySDK(MyApp.getApp());
            sdk.startRawLauncher();
            return true;
        } catch (Throwable e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setAnim(Context c, String path) {
        try {
            EnjoySDK sdk = new EnjoySDK(c.getApplicationContext());
            long start = System.currentTimeMillis();
            int r = sdk.setBootanimation(path);
            return true;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return false;
    }
}
